package doc2db.interfaces.action.save;

import doc2db.fileWorker.FileIO;
import doc2db.interfaces.mainWindow.MyJEditorPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author yura
 *
 * Html, Xls, Txt, Doc
 *
 */
public class SaveAsAction implements ActionListener{
    /** File filter - "HTML document" */
    public static final String HTML_DOCUMENT = "HTML document (.html)";
    /** File filter - "Microsoft Word document" */
    public static final String MICROSOFT_WORD_DOCUMENT = "Microsoft Word document (.doc)";
    /** File filter - "Microsoft Word document" */
    public static final String WORD_2007_DOCUMENT = "Word 2007 document (.docx)";
    /** File filter - "Text document" */
    public static final String TEXT_DOCUMENT = "Text document (.txt)";
    /** File filter - "Xls document" */
    public static final String XLS_DOCUMENT = "Xls document (.xls)";

    /** Edit panel whith date */
    private MyJEditorPane medit ;

    /** Text of error if it hapen */
    private String error = "ancnown";

    public SaveAsAction(MyJEditorPane medit) {
        this.medit = medit;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        JFileChooser chooser = new JFileChooser();
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        chooser.setDialogTitle("doc2db:Сохранить как");
        chooser.setCurrentDirectory(new File("."));
        String[] e4 = new String[]{"htm", "HTM", "html", "HTML"};
        chooser.setFileFilter(new FileNameExtensionFilter(HTML_DOCUMENT, e4));
        String[] e2 = new String[]{"xls", "XLS"};
        chooser.setFileFilter(new FileNameExtensionFilter(XLS_DOCUMENT, e2));
        String[] e3 = new String[]{"txt", "TXT"};
        chooser.setFileFilter(new FileNameExtensionFilter(TEXT_DOCUMENT, e3));
        String[] e5 = new String[]{"docx", "DOCX"};
        chooser.setFileFilter(new FileNameExtensionFilter(WORD_2007_DOCUMENT, e5));
        String[] e1 = new String[]{"doc", "DOC"};
        chooser.setFileFilter(new FileNameExtensionFilter(MICROSOFT_WORD_DOCUMENT, e1));

        int r = chooser.showOpenDialog(null);
        String s = chooser.getFileFilter().getDescription();
        Saver save = null;

        if (r == JFileChooser.APPROVE_OPTION) {
            String fname = null;
            try {
                fname  = chooser.getSelectedFile().getAbsolutePath();
            } catch (Exception ex) {
                err("can save like \n" + ex);
                return;
            }
            if (s.equals(HTML_DOCUMENT)){
                if (! saveHtml(fname)){
                    err("can save like html-file:\n" + fname + "\nerror - " + error);
                }
            } else if (s.equals(XLS_DOCUMENT)){
                save = new SaveXls(medit);
                if (! save.save(fname)){
                    err("can save like xl-file:\n" + fname + "\nerror - " + save.err());
                }
            } else if (s.equals(TEXT_DOCUMENT)){
                save = new SaveTxt(medit);
                if (! save.save(fname)){
                    err("can save like text-file:\n" + fname + "\nerror - " + save.err());
                }
            } else if (s.equals(MICROSOFT_WORD_DOCUMENT)){
                save = new SaveDoc(medit);
                if (! save.save(fname)){
                    err("can save like doc-file:\n" + fname + "\nerror - " + save.err());
                }
            } else if (s.equals(WORD_2007_DOCUMENT)){
                save = new SaveDocx(medit);
                if (! save.save(fname)){
                    err("can save like docx-file:\n" + fname + "\nerror - " + save.err());
                }
            } else {
                if (! saveAny(fname)){
                    err("can save file:\n" + fname + "\nerror - " + error);
                }
            }
        }
    }

    private boolean saveHtml (String fname){
        String mfname;
        if (new File(fname).exists()){
            mfname = "" + fname;
        } else if (fname.endsWith(".htm") | fname.endsWith(".HTM") |
                fname.endsWith(".html") | fname.endsWith(".HTML")) {
            mfname = "" + fname;
        } else {
            mfname = fname + ".html";
        }
        boolean ret = FileIO.createFile(medit.getText(), mfname);
        if (! ret){
            error = "file IO exception";
        }
        return ret;
    }

    private boolean saveAny (String fname){
        boolean ret = false;
        if (fname.endsWith(".htm") | fname.endsWith(".HTM") |
                fname.endsWith(".html") | fname.endsWith(".HTML")) {
            return saveHtml(fname);
        } else if (fname.endsWith(".doc") | fname.endsWith(".DOC")) {
            SaveDoc s = new SaveDoc(medit);
            ret = s.save(fname);
            error = s.err();
            return ret;
        } else if (fname.endsWith(".docx") | fname.endsWith(".DOCX")) {
            SaveDocx s = new SaveDocx(medit);
            ret = s.save(fname);
            error = s.err();
            return ret;
        } else if (fname.endsWith(".xls") | fname.endsWith(".XLS")){
            SaveXls s = new SaveXls(medit);
            ret = s.save(fname);
            error = s.err();
            return ret;
        } else if (fname.endsWith(".txt") | fname.endsWith(".TXT")) {
            SaveTxt s = new SaveTxt(medit);
            ret = s.save(fname);
            error = s.err();
            return ret;
        } else {
            error = "wrong file format";
            return ret;
        }
    }

    /**
     * Show error message box whith text "text"
     * @param text text of message box
     */
    private void err(String text){
        JOptionPane.showMessageDialog(medit, text, "error in saving",
                JOptionPane.ERROR_MESSAGE);
    }

}
